package com.flurry.sdk;

import com.flurry.sdk.ip;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public final class it {
    private static final String e = iu.class.getName();
    public long a;
    /* access modifiers changed from: package-private */
    public int b;
    public String c;
    /* access modifiers changed from: package-private */
    public Map<Long, ip> d;
    /* access modifiers changed from: private */
    public long f = System.currentTimeMillis();
    /* access modifiers changed from: private */
    public long g;
    /* access modifiers changed from: private */
    public ix h;
    /* access modifiers changed from: private */
    public boolean i;
    /* access modifiers changed from: private */
    public int j;
    /* access modifiers changed from: private */
    public AtomicInteger k;

    public it(String str, boolean z, long j2, long j3, ix ixVar, Map<Long, ip> map) {
        this.c = str;
        this.i = z;
        this.a = j2;
        this.g = j3;
        this.h = ixVar;
        this.d = map;
        if (map != null) {
            for (Long l : map.keySet()) {
                map.get(l).m = this;
            }
            this.j = map.size();
        } else {
            this.j = 0;
        }
        this.k = new AtomicInteger(0);
    }

    public final List<ip> a() {
        if (this.d != null) {
            return new ArrayList(this.d.values());
        }
        return Collections.emptyList();
    }

    public final synchronized boolean b() {
        return this.k.intValue() >= this.j;
    }

    public final synchronized void c() {
        this.k.incrementAndGet();
    }

    public static class a implements lg<it> {
        lf<ip> a = new lf<>(new ip.a());

        public final /* synthetic */ Object a(InputStream inputStream) throws IOException {
            if (inputStream == null) {
                return null;
            }
            AnonymousClass2 r0 = new DataInputStream(inputStream) {
                public final void close() {
                }
            };
            long readLong = r0.readLong();
            long readLong2 = r0.readLong();
            long readLong3 = r0.readLong();
            ix a2 = ix.a(r0.readInt());
            boolean readBoolean = r0.readBoolean();
            int readInt = r0.readInt();
            String readUTF = r0.readUTF();
            int readInt2 = r0.readInt();
            int readInt3 = r0.readInt();
            it itVar = new it(readUTF, readBoolean, readLong, readLong3, a2, (Map<Long, ip>) null);
            long unused = itVar.f = readLong2;
            int unused2 = itVar.b = readInt;
            int unused3 = itVar.j = readInt2;
            AtomicInteger unused4 = itVar.k = new AtomicInteger(readInt3);
            List<ip> b = this.a.a(inputStream);
            if (b != null) {
                Map unused5 = itVar.d = new HashMap();
                for (ip next : b) {
                    next.m = itVar;
                    itVar.d.put(Long.valueOf(next.b), next);
                }
            }
            return itVar;
        }

        public final /* synthetic */ void a(OutputStream outputStream, Object obj) throws IOException {
            it itVar = (it) obj;
            if (outputStream != null && itVar != null) {
                AnonymousClass1 r0 = new DataOutputStream(outputStream) {
                    public final void close() {
                    }
                };
                r0.writeLong(itVar.a);
                r0.writeLong(itVar.f);
                r0.writeLong(itVar.g);
                r0.writeInt(itVar.h.e);
                r0.writeBoolean(itVar.i);
                r0.writeInt(itVar.b);
                if (itVar.c != null) {
                    r0.writeUTF(itVar.c);
                } else {
                    r0.writeUTF("");
                }
                r0.writeInt(itVar.j);
                r0.writeInt(itVar.k.intValue());
                r0.flush();
                this.a.a(outputStream, itVar.a());
            }
        }
    }

    public final byte[] d() throws IOException {
        DataOutputStream dataOutputStream;
        DataOutputStream dataOutputStream2 = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            try {
                dataOutputStream.writeShort(this.h.e);
                dataOutputStream.writeLong(this.a);
                dataOutputStream.writeLong(this.g);
                dataOutputStream.writeBoolean(this.i);
                if (this.i) {
                    dataOutputStream.writeShort(this.b);
                    dataOutputStream.writeUTF(this.c);
                }
                dataOutputStream.writeShort(this.d.size());
                if (this.d != null) {
                    for (Map.Entry next : this.d.entrySet()) {
                        ip ipVar = (ip) next.getValue();
                        dataOutputStream.writeLong(((Long) next.getKey()).longValue());
                        dataOutputStream.writeUTF(ipVar.r);
                        dataOutputStream.writeShort(ipVar.a.size());
                        Iterator<iq> it = ipVar.a.iterator();
                        while (it.hasNext()) {
                            iq next2 = it.next();
                            dataOutputStream.writeShort(next2.a);
                            dataOutputStream.writeLong(next2.b);
                            dataOutputStream.writeLong(next2.c);
                            dataOutputStream.writeBoolean(next2.d);
                            dataOutputStream.writeShort(next2.e);
                            dataOutputStream.writeShort(next2.f.e);
                            if ((next2.e < 200 || next2.e >= 400) && next2.g != null) {
                                byte[] bytes = next2.g.getBytes();
                                dataOutputStream.writeShort(bytes.length);
                                dataOutputStream.write(bytes);
                            }
                            dataOutputStream.writeShort(next2.h);
                            dataOutputStream.writeInt((int) next2.k);
                        }
                    }
                }
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                ly.a((Closeable) dataOutputStream);
                return byteArray;
            } catch (IOException e2) {
                e = e2;
                dataOutputStream2 = dataOutputStream;
                try {
                    km.a(6, e, "Error when generating report", e);
                    throw e;
                } catch (Throwable th) {
                    th = th;
                    dataOutputStream = dataOutputStream2;
                    ly.a((Closeable) dataOutputStream);
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
                ly.a((Closeable) dataOutputStream);
                throw th;
            }
        } catch (IOException e3) {
            e = e3;
            km.a(6, e, "Error when generating report", e);
            throw e;
        } catch (Throwable th3) {
            th = th3;
            dataOutputStream = null;
            ly.a((Closeable) dataOutputStream);
            throw th;
        }
    }
}
